package;

import nme.display.MovieClip;
import nme.display.SimpleButton;
import nme.events.MouseEvent;
import nme.display.Bitmap;
import nme.ui.Mouse;
import nme.events.Event;

/**
 * @author jonathanmoriarty
 */
class SelectScreen extends MovieClip, implements Screen
{
	// TODO: Add an animated background.
	private var _background:Bitmap;
	
	private var _player1Display:Bitmap;
	inline private static var PLAYER_1_DISPLAY_X:Int = 280;
	inline private static var PLAYER_1_DISPLAY_Y:Int = 365;
	
	private var _player2Display:Bitmap;
	inline private static var PLAYER_2_DISPLAY_X:Int = 373;
	inline private static var PLAYER_2_DISPLAY_Y:Int = 365;
	
	public var _selected0:Int;
	public var _selected1:Int;
	
	private var _playButton:SimpleButton;
	inline private static var PLAY_BUTTON_X:Int = 330;
	inline private static var PLAY_BUTTON_Y:Int = 200;
	
	private var _menuButton:SimpleButton;
	inline private static var MENU_BUTTON_X:Int = 330;
	inline private static var MENU_BUTTON_Y:Int = 300;
	
	private var _character0Select:SimpleButton;
	inline private static var ZERO_BUTTON_X:Int = 323;
	inline private static var ZERO_BUTTON_Y:Int = 46;
	
	private var _character1Select:SimpleButton;
	inline private static var ONE_BUTTON_X:Int = 256;
	inline private static var ONE_BUTTON_Y:Int = 79;
	
	private var _character2Select:SimpleButton;
	inline private static var TWO_BUTTON_X:Int = 388;
	inline private static var TWO_BUTTON_Y:Int = 79;
	
	private var _randomSelect:SimpleButton;
	inline private static var RANDOM_BUTTON_X:Int = 323;
	inline private static var RANDOM_BUTTON_Y:Int = 112;
	
	private var _portrait0:CharacterPortrait;
	inline private static var PORTRAIT_0_X:Int = 17;
	inline private static var PORTRAIT_0_Y:Int = 80;
	
	private var _portrait1:CharacterPortrait;
	inline private static var PORTRAIT_1_X:Int = 703;
	inline private static var PORTRAIT_1_Y:Int = 80;
	
	public function new() 
	{
		super();
		
		_background = Tools.loadBitmap( "select_screen" );
		_background.x = 0;
		_background.y = 0;
		addChild(_background);
		
		_player1Display = Tools.loadBitmap( "player1" );
		_player1Display.x = PLAYER_1_DISPLAY_X;
		_player1Display.y = PLAYER_1_DISPLAY_Y;
		addChild(_player1Display);
		
		_player2Display = Tools.loadBitmap( "player2" );
		_player2Display.x = PLAYER_2_DISPLAY_X;
		_player2Display.y = PLAYER_2_DISPLAY_Y;
		addChild(_player2Display);
		
		_selected0 = -1;
		_selected1 = -1;
		
		_makePlayButton();
		_makeMenuButton();
		_makeCharacter0Button();
		_makeCharacter1Button();
		_makeCharacter2Button();
		_makeRandomButton();
		
		_makePortrait0();
		_makePortrait1();
		
		Mouse.show();
		show();
	}
	
	private function _makePortrait0():Void
	{
		_portrait0 = new CharacterPortrait( PORTRAIT_0_X, PORTRAIT_0_Y, 1 );
		addChild( _portrait0 );
	}
	
	private function _makePortrait1():Void
	{
		_portrait1 = new CharacterPortrait( PORTRAIT_1_X, PORTRAIT_1_Y, -1 );
		addChild( _portrait1 );
		_portrait1.setName( -1 );
	}
	
	private function _makePlayButton():Void
	{
		_playButton = new SimpleButton();
		_playButton.upState = Tools.loadBitmap( "play_button_up" );
		_playButton.overState = Tools.loadBitmap( "play_button_up" );
		_playButton.downState = Tools.loadBitmap( "play_button_down" );
		_playButton.hitTestState = _playButton.overState;
		_playButton.x = PLAY_BUTTON_X;
		_playButton.y = PLAY_BUTTON_Y;
		addChild( _playButton );
	}
	
	private function _makeMenuButton():Void
	{
		_menuButton = new SimpleButton();
		_menuButton.upState = Tools.loadBitmap( "menu_button_up" );
		_menuButton.overState = Tools.loadBitmap( "menu_button_up" );
		_menuButton.downState = Tools.loadBitmap( "menu_button_down" );
		_menuButton.hitTestState = _menuButton.overState;
		_menuButton.x = MENU_BUTTON_X;
		_menuButton.y = MENU_BUTTON_Y;
		addChild( _menuButton );
	}
	
	private function _makeRandomButton():Void
	{
		_randomSelect = new SimpleButton();
		_randomSelect.upState = Tools.loadBitmap( "select_random_button_up" );
		_randomSelect.overState = Tools.loadBitmap( "select_random_button_up" );
		_randomSelect.downState = Tools.loadBitmap( "select_random_button_up" );
		_randomSelect.hitTestState = _randomSelect.overState;
		_randomSelect.x = RANDOM_BUTTON_X;
		_randomSelect.y = RANDOM_BUTTON_Y;
		addChild( _randomSelect );
	}
	
	private function _makeCharacter0Button():Void
	{
		_character0Select = new SimpleButton();
		_character0Select.upState = Tools.loadBitmap( "select_0_button_up" );
		_character0Select.overState = Tools.loadBitmap( "select_0_button_up" );
		_character0Select.downState = Tools.loadBitmap( "select_0_button_up" );
		_character0Select.hitTestState = _character0Select.overState;
		_character0Select.x = ZERO_BUTTON_X;
		_character0Select.y = ZERO_BUTTON_Y;
		addChild( _character0Select );
	}
	
	private function _makeCharacter1Button():Void
	{
		_character1Select = new SimpleButton();
		_character1Select.upState = Tools.loadBitmap( "select_1_button_up" );
		_character1Select.overState = Tools.loadBitmap( "select_1_button_up" );
		_character1Select.downState = Tools.loadBitmap( "select_1_button_up" );
		_character1Select.hitTestState = _character1Select.overState;
		_character1Select.x = ONE_BUTTON_X;
		_character1Select.y = ONE_BUTTON_Y;
		addChild( _character1Select );
	}
	
	private function _makeCharacter2Button():Void
	{
		_character2Select = new SimpleButton();
		_character2Select.upState = Tools.loadBitmap( "select2_button_up_locked" );
		_character2Select.overState = Tools.loadBitmap( "select2_button_up_locked" );
		_character2Select.downState = Tools.loadBitmap( "select2_button_up_locked" );
		_character2Select.hitTestState = _character2Select.overState;
		_character2Select.x = TWO_BUTTON_X;
		_character2Select.y = TWO_BUTTON_Y;
		addChild( _character2Select );
	}
	
	public function _onClickMenu( l_event:MouseEvent ):Void
	{
		dispatchEvent( new NavigationEvent( NavigationEvent.MENU ) );
	}
	
	public function _onClickPlay( l_event:MouseEvent ):Void
	{
		if ( _selected0 != -1 )
		{
			_selected1 = Math.round( Math.random() * 1 );
			_portrait1.changeImage("portrait_" + _selected1 );
			_portrait1.setName(_selected1);
		
			dispatchEvent( new NavigationEvent( NavigationEvent.PLAY ) );
		}
	}
	
	public function _onClickSelect0( l_event:MouseEvent ):Void
	{
		_selected0 = 0;
		_portrait0.changeImage("portrait_0");
		_portrait0.setName(_selected0);
	}
	
	public function _onClickSelect1( l_event:MouseEvent ):Void
	{
		_selected0 = 1;
		_portrait0.changeImage("portrait_1");
		_portrait0.setName(_selected0);
	}
	
	public function _onClickSelect2( l_event:MouseEvent ):Void
	{
		_selected0 = 2;
		_portrait0.changeImage("portrait_2");
		_portrait0.setName(_selected0);
	}
	
	public function _onClickSelectRandom( l_event:MouseEvent ):Void
	{
		_selected0 = Math.round( Math.random() * 1 );
		_portrait0.changeImage( "portrait_null" );
		_portrait0.setName(-1);
	}
	
	public function show():Void
	{
		this.visible = true;
		_playButton.addEventListener( MouseEvent.CLICK, _onClickPlay );
		_menuButton.addEventListener( MouseEvent.CLICK, _onClickMenu );
		_character0Select.addEventListener( MouseEvent.CLICK, _onClickSelect0 );
		_character1Select.addEventListener( MouseEvent.CLICK, _onClickSelect1 );
		//_character2Select.addEventListener( MouseEvent.CLICK, _onClickSelect2 );
		_randomSelect.addEventListener( MouseEvent.CLICK, _onClickSelectRandom );
	}
	
	public function hide():Void
	{
		this.visible = false;
		if ( _playButton.hasEventListener( MouseEvent.CLICK ) )
		{
			_playButton.removeEventListener( MouseEvent.CLICK, _onClickPlay );
		}
		if ( _menuButton.hasEventListener( MouseEvent.CLICK ) )
		{
			_menuButton.removeEventListener( MouseEvent.CLICK, _onClickMenu );
		}
		if ( _character0Select.hasEventListener( MouseEvent.CLICK ) )
		{
			_character0Select.addEventListener( MouseEvent.CLICK, _onClickSelect0 );
		}
		if ( _character1Select.hasEventListener( MouseEvent.CLICK ) )
		{
			_character1Select.addEventListener( MouseEvent.CLICK, _onClickSelect1 );
		}
		if ( _character2Select.hasEventListener( MouseEvent.CLICK ) )
		{
			_character2Select.addEventListener( MouseEvent.CLICK, _onClickSelect2 );
		}
		if ( _randomSelect.hasEventListener( MouseEvent.CLICK ) )
		{
			_randomSelect.addEventListener( MouseEvent.CLICK, _onClickSelectRandom );
		}
	}
	
	public function update( l_event:Event ):Void 
	{	
	
	}
	
	public function reset():Void
	{
		// nothing needed here at the moment.
	}
	
	public function destroy():Void
	{
		hide();
		removeChild( _playButton );
		_playButton = null;
		removeChild( _menuButton );
		_menuButton = null;
	}

}